from flask import Flask
from flask import render_template
import requests
import pandas as pd
from flask import request
from flask import jsonify

import SQL

class CustomFlask(Flask):
    jinja_options = Flask.jinja_options.copy()
    jinja_options.update(dict(
        variable_start_string='%%',  # Default is '{{', I'm changing this because Vue.js uses '{{' / '}}'
        variable_end_string='%%',
    ))


app = CustomFlask(__name__)

sql = SQL.SQL('remote', 'weather_base', 'root', 'CODEcode-123')


@app.route('/')
def hello_world():
    return render_template('index.html')


@app.route('/get_weather_by_city/<city>')
def get_weather_by_city(city):
    return sql.get_weather_by_city(city)


@app.route('/get_weather_by_area/<area>')
def get_weather_by_area(area):
    return sql.get_weather_by_area(area)


@app.route('/get_weather_by_area_and_date/<area>/<date>')
def get_weather_by_area_and_date(area, date):
    return sql.get_weather_by_area_and_date(area, date)


@app.route('/get_all_city')
def get_all_city():
    city_json_url = 'https://j.i8tq.com/weather2020/search/city.js'
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
    }
    response = requests.get(city_json_url, headers=headers)
    print("状态码:" + str(response.status_code))  # 打印状态码
    if response.status_code != 200:
        print("获取城市信息失败")
        return None

    return response.text[response.text.find('{'):]


@app.route('/get_map_weather_max_temp')
def get_map_weather_max_temp():
    return sql.get_map_weather_max_temp()


@app.route('/get_city_with_biggest_temp_diff')
def get_city_with_biggest_temp_diff():
    return sql.get_city_with_biggest_temp_diff()


if __name__ == '__main__':
    app.run()
